package com.brainshare.web.controller.an;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.brainshare.an.domain.Exercise;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.brainshare.common.annotation.Log;
import com.brainshare.common.core.controller.BaseController;
import com.brainshare.common.core.domain.AjaxResult;
import com.brainshare.common.enums.BusinessType;
import com.brainshare.an.domain.ExerciseProduct;
import com.brainshare.an.service.IExerciseProductService;
import com.brainshare.common.utils.poi.ExcelUtil;
import com.brainshare.common.core.page.TableDataInfo;

/**
 * 训练产品Controller
 * 
 * @author wolong
 * @date 2023-03-10
 */
@RestController
@RequestMapping("/an/exerciseProduct")
public class ExerciseProductController extends BaseController
{
    @Autowired
    private IExerciseProductService exerciseProductService;

    /**
     * 查询训练产品列表
     */
//    @PreAuthorize("@ss.hasPermi('exercise:product:list')")
    @GetMapping("/list")
    public TableDataInfo list(ExerciseProduct exerciseProduct)
    {
        startPage();
        List<ExerciseProduct> list = exerciseProductService.selectExerciseProductList(exerciseProduct);
        return getDataTable(list);
    }

    /**
     * 导出训练产品列表
     */
//    @PreAuthorize("@ss.hasPermi('exercise:product:export')")
//    @Log(title = "训练产品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExerciseProduct exerciseProduct)
    {
        List<ExerciseProduct> list = exerciseProductService.selectExerciseProductList(exerciseProduct);
        ExcelUtil<ExerciseProduct> util = new ExcelUtil<ExerciseProduct>(ExerciseProduct.class);
        util.exportExcel(response, list, "训练产品数据");
    }

    /**
     * 获取训练产品详细信息
     */
//    @PreAuthorize("@ss.hasPermi('exercise:product:query')")
    @GetMapping(value = "/{exerciseProductId}")
    public AjaxResult getInfo(@PathVariable("exerciseProductId") Long exerciseProductId)
    {
        return success(exerciseProductService.selectExerciseProductByExerciseProductId(exerciseProductId));
    }

    /**
     * 新增训练产品
     */
//    @PreAuthorize("@ss.hasPermi('exercise:product:add')")
//    @Log(title = "训练产品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ExerciseProduct exerciseProduct)
    {
        return toAjax(exerciseProductService.insertExerciseProduct(exerciseProduct));
    }

    /**
     * 修改训练产品
     */
//    @PreAuthorize("@ss.hasPermi('exercise:product:edit')")
//    @Log(title = "训练产品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ExerciseProduct exerciseProduct)
    {
        return toAjax(exerciseProductService.updateExerciseProduct(exerciseProduct));
    }

    /**
     * 删除训练产品
     */
//    @PreAuthorize("@ss.hasPermi('exercise:product:remove')")
//    @Log(title = "训练产品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{exerciseProductIds}")
    public AjaxResult remove(@PathVariable Long[] exerciseProductIds)
    {
        return toAjax(exerciseProductService.deleteExerciseProductByExerciseProductIds(exerciseProductIds));
    }

    @PostMapping("/createProduct")
    public AjaxResult createProduct()
    {
        ExerciseProduct exerciseProduct = new ExerciseProduct();
        exerciseProduct.setName("day1");
        exerciseProduct.setDescription("第一天训练");
        Exercise exercise = new Exercise();
        exercise.setDescription("蔬果大战的描述");
        exercise.setName("蔬果大战");
        exercise.setUrl("www.baidu.com");
        List<Exercise> lstExercise = new ArrayList<>();
        lstExercise.add(exercise);
        exerciseProduct.setExerciseList(lstExercise);
        int rows = 0;
        rows = exerciseProductService.insertExerciseProduct(exerciseProduct);
        if (rows > 0)
        {
            return AjaxResult.success();
        } else
        {
            return AjaxResult.error();
        }
    }
}
